/** COPYRIGHT(C) 2008. Loc Phan. All Rights Reserved.
    A class that check if a sentence is profane.
    @author Loc Phan
    @version 1.01 2008-10-28
*/
public class Sentence
{
   /**Constructs a sentence
    * 
    * @param input a Sentence
    */
   public Sentence(String input)
   {
      myString = input.toLowerCase();
   }
   /**Check if sentence is profane
    * 
    * @return True if sentence is profane
    */
   public boolean isProfane()
   {
      int spaces = CountSpaces(myString);
      int i=0;
      String substring;
      String substring2 = myString;
      if (spaces>0)
      {
         while(i<spaces)
         {
            substring = substring2.substring(0, substring2.indexOf(" "));
            isProfane = ContainsBadWord(substring);
            if(isProfane)
            {
               break;
            }
            substring2=substring2.substring(substring2.indexOf(" ")+1);
            if((i==spaces-1))
               {
                  isProfane = ContainsBadWord(substring2);
               }
            i++;
         }
      }
      else
      {
         isProfane = ContainsBadWord(myString);
      }
      return isProfane;
           
   }
   /**Count spaces in a string
    * 
    * @param input String
    * @return Number of space(s)
    */
   public int CountSpaces(String input)
   {
      int spaces=0;
      for(int counter=0;counter<input.length();counter++)
      {
         if (input.charAt(counter)==' ')
            spaces++;
      }
      return spaces;
   }
   /**Check if a word is bad word
    * 
    * @param input a word
    * @return True if word is a bad word
    */
   private boolean ContainsBadWord(String input)
   {
      boolean answer = false;
      if (input.length()>3)
      {
         answer = (input.contains(" cat")||input.contains("cat ")
              ||input.contains(" dog")||input.contains("dog ")
              ||input.contains("llama"));
      }
      else 
      {
         answer = (input.contains("cat")||input.contains("dog"));
      }
      return answer;
   }
   private String myString;
   private boolean isProfane;
}
